package com.tju.shortlink.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.shortlink.project.dao.entity.LinkDo;
import com.tju.shortlink.project.dao.mapper.LinkMapper;
import com.tju.shortlink.project.dto.req.RecycleBinRemoveDTO;
import com.tju.shortlink.project.dto.req.RecycleBinSaveDTO;
import com.tju.shortlink.project.dto.req.ShortLinkRecycleBinPageReqDTO;
import com.tju.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.tju.shortlink.project.service.IRecycleBinService;
import com.tju.shortlink.project.toolkit.LinkUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static com.tju.shortlink.project.common.constant.RedisKeyConstant.*;

/**
 * 回收站实现类
 */
@Service
@AllArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<LinkMapper, LinkDo> implements IRecycleBinService {

    private final StringRedisTemplate redisTemplate;

    @Override
    public void saveRecycleBin(RecycleBinSaveDTO requestParam) {
        // 1. 封装数据
        String fullShortUrl = requestParam.getFullShortUrl();
        LambdaUpdateWrapper<LinkDo> updateWrapper = Wrappers.<LinkDo>lambdaUpdate()
                .eq(LinkDo::getFullShortUrl, fullShortUrl)
                .eq(LinkDo::getGid, requestParam.getGid())
                .eq(LinkDo::getEnableStatus, 0)
                .eq(LinkDo::getDelFlag, 0);
        LinkDo linkDo = LinkDo.builder().enableStatus(1).build();
        // 2. 重置缓存 删除短链接缓存， 添加空值缓存
        redisTemplate.delete(String.format(GOTO_SHORT_LINK_KEY, fullShortUrl));
        redisTemplate.opsForValue().set(String.format(GOTO_IS_NULL_SHORT_LINK_KEY, fullShortUrl), "-", 30, TimeUnit.MINUTES);
        baseMapper.update(linkDo, updateWrapper);
    }

    @Override
    public IPage<ShortLinkPageRespDTO> pageShortLink(ShortLinkRecycleBinPageReqDTO requestParam) {
        // 1. 查询db获得分页数据
        IPage<LinkDo> page = baseMapper.pageRecycleBinLink(requestParam);
        // 2. 封装结果并返回
        IPage<ShortLinkPageRespDTO> result = page.convert(each -> {
            ShortLinkPageRespDTO bean = BeanUtil.toBean(each, ShortLinkPageRespDTO.class);
            bean.setDomain("http://"+bean.getDomain());
            if(StrUtil.isNotBlank(bean.getFavicon())){
                bean.setFavicon(redisTemplate.opsForValue().get(SHORT_LINK_FAVICON_KEY+bean.getFavicon()));
            }
            return bean;
        });
        return result;
    }

    @Override
    public void recoveryRecycleBin(RecycleBinSaveDTO requestParam) {
        LambdaUpdateWrapper<LinkDo> updateWrapper = Wrappers.<LinkDo>lambdaUpdate()
                .eq(LinkDo::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(LinkDo::getGid, requestParam.getGid())
                .eq(LinkDo::getEnableStatus, 1)
                .eq(LinkDo::getDelFlag, 0);
        LinkDo linkDo = LinkDo.builder().enableStatus(0).build();
        baseMapper.update(linkDo, updateWrapper);
        redisTemplate.delete(String.format(GOTO_IS_NULL_SHORT_LINK_KEY, requestParam.getFullShortUrl()));
    }

    @Override
    public void removeRecycleBin(RecycleBinRemoveDTO requestParam) {
        LambdaUpdateWrapper<LinkDo> updateWrapper = Wrappers.<LinkDo>lambdaUpdate()
                .eq(LinkDo::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(LinkDo::getGid, requestParam.getGid())
                .eq(LinkDo::getEnableStatus, 1)
                .eq(LinkDo::getDelFlag, 0);
        LinkDo delShortLinkDO = LinkDo.builder()
                .delTime(System.currentTimeMillis())
                .build();
        delShortLinkDO.setDelFlag(1);
        baseMapper.update(delShortLinkDO, updateWrapper);
    }
}
